package edu.sysu.cshare.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.sysu.cshare.model.SubmitInfo;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.service.SubmitInfoService;
import edu.sysu.cshare.service.UserService;
import edu.sysu.cshare.util.Mis;

@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport {

	private UserService userService;
	private String userId;
	private String password;
	private String userNumMsg;
	private String passwordMsg;
	private User user;
	private boolean showUnDone;

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public User getUser() {
		return user;
	}

	public String getUserNumMsg() {
		return userNumMsg;
	}

	public String getPasswordMsg() {
		return passwordMsg;
	}

	public boolean getShowUnDone() {
		return showUnDone;
	}

	public void setShowUnDone(boolean showUnDone) {
		this.showUnDone = showUnDone;
	}

	public String login() {
		user = userService.get(userId);
		if (user == null) {
			userNumMsg = "不存在这个学号";
			return ERROR;
		}
		if (!Mis.getMD5(password).equals(user.getPassword())) {
			passwordMsg = "密码不正确";
			return ERROR;
		}
		user.setLastIP(org.apache.struts2.ServletActionContext.getRequest()
				.getRemoteAddr());
		userService.update(user);
		return SUCCESS;
	}

}
